<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <el-form label-width="100px">
          <el-form-item label="线路："><xl ref="xlForm" @change="change(arguments)" /></el-form-item>
        </el-form>
      </div>
      <div class="filter-item">
        <el-button v-waves :loading="listLoading" class="filter-item" type="primary" size="small" icon="el-icon-search" @click="handleSearch">查询</el-button>
      </div>
      <div class="filter-item">
        <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" size="small" icon="el-icon-download" @click="handleDownload">导出</el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" :data="list" size="mini" fit highlight-current-row style="width: 100%;" max-height="500">
      <el-table-column label="站点序号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.xh }}</span>
        </template>
      </el-table-column>
      <el-table-column label="站点名称" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.zdmc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上下行" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.sxx }}</span>
        </template>
      </el-table-column>
      <el-table-column label="站点类型" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.zdlx }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-class">
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { getXlzdPage, getXlzdOutput } from '@/api/xlzdgl';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import { formatJson } from '@/utils';
import Xl from '@/views/database/xl.vue';

export default {
  name: 'Xlzdgl',
  components: { Pagination, Xl },
  directives: { waves },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      downloadLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        jgxlid: null
      }
    };
  },
  created() {
    this.listLoading = false;
  },
  methods: {
    async getList() {
      if (this.listQuery.jgxlid === null) {
        this.$message.error('请选择线路');
        return;
      }
      this.listLoading = true;
      await getXlzdPage(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    change(data) {
      this.listQuery.jgxlid = data[0];
    },
    async handleDownload() {
      if (this.total === 0) {
        this.$message.error('暂无数据');
        return;
      }
      this.downloadLoading = true;
      await getXlzdOutput(this.listQuery).then(response => {
        if (this.listQuery.jgxlid === null) {
          this.$message.error('请选择线路');
          return;
        }
        if (response.data.length === 0) {
          this.$message.error('暂无数据，请重新刷新页面');
          return;
        }
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['站点序号', '站点名称', '上下行', '站点类型'];
          const filterVal = ['xh', 'zdmc', 'sxx', 'zdlx'];
          const data = formatJson(response.data, filterVal);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '线路站点信息'
          });
          this.downloadLoading = false;
          this.$message({
            message: '线路站点信息导出成功',
            type: 'success'
          });
        });
      });
    }
  }
};
</script>
